package com.example.huhaichao.music.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.example.huhaichao.music.R;

import com.example.huhaichao.music.adapter.ViewAdapter;
import com.example.huhaichao.music.util.MusicUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huhaichao on 2017/5/1.
 */

public class SearchMusic extends Fragment {
    private ListView list_view;
    private EditText search;
    public Callbacks3 mCallbacks3;
    private ViewAdapter mAdapter;
    private static final String TAG = "SearchMusic";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_search_music, container, false);
        list_view = (ListView) v.findViewById(R.id.list_view);
        mAdapter = new ViewAdapter(getActivity(),MusicUtils.aMusicList);
        list_view.setAdapter(mAdapter);
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                Log.d(TAG, "onItemClick: " + position);
                mCallbacks3.onItemSelectedSearch(position);
            }
        });
        search = (EditText) v.findViewById(R.id.search);
        //搜索关键字
        search.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String a = search.getText().toString();
                //调用适配器里面的搜索方法
                mAdapter.SearchMusic(a);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //如果activity没有实现Callbacks接口，抛出异常
        if (!(activity instanceof Callbacks3)) {
            throw new IllegalStateException("没有实现接口");
        }
        mCallbacks3 = (Callbacks3) activity;
    }
    public interface Callbacks3 {
        void onItemSelectedSearch(int id);
    }
}
